axios.defaults.baseURL = "http://localhost:8080"  //配置跟路径
Vue.prototype.$http = axios  //将axios交个vue实例

//前置拦截器
axios.interceptors.request.use(config => {
    let uToken = localStorage.getItem("token")
    if (uToken) {
        config.headers["U-TOKEN"] = uToken;
    }
    return config;
}, error => {
    Promise.reject(error)
})

//后置拦截器
axios.interceptors.response.use(result => {
    let data = result.data;
    if (!data.success && data.resultObj === "nologin")
        location.href = "login.html"
    return result;
}, error => {
    Promise.reject(error);
})

//静态资源拦截器
let url = location.href;
if (url.indexOf("login.html") > 0 || url.indexOf("register.html") > 0) {
    localStorage.removeItem('logininfo');
    localStorage.removeItem('token');
}
let user = JSON.parse(localStorage.getItem('logininfo'));
/* 如果不是登录信息并且要访问的除登录接口和用户注册接口和首页接口以外的接口 那么就需要跳转到登录界面 */
if (!user && url.indexOf("login.html") === -1
    && url.indexOf("register.html") === -1
    && url.indexOf("index.html") === -1
    && url.indexOf("binder.html") === -1
    && url.indexOf("callback.html") === -1
) {
    location.href = "login.html"
}

//从url中获取参数对象
function parseUrlParams2Obj(url) {//http://bugtracker.itsource.cn/?code=071m46ll2v5Rv94Hdxnl2Ux7ZB1m46lh&state=1
    let paramStr = url.substring(url.indexOf("?") + 1);//code=071m46ll2v5Rv94Hdxnl2Ux7ZB1m46lh&state=1
    let paramStrs = paramStr.split("&");//[code=071m46ll2v5Rv94Hdxnl2Ux7ZB1m46lh,state=1]
    let paramObj = {};
    for (let i = 0; i < paramStrs.length; i++) {
        let paramName = paramStrs[i].split("=")[0];//code = 071m46ll2v5Rv94Hdxnl2Ux7ZB1m46lh
        let paramValue = paramStrs[i].split("=")[1];
        paramObj[paramName] = paramValue;
    }
    return paramObj
}